﻿<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false" extends="fileManager.util.AbsService" >

	<cfset variables.STATIC.FILE_FORMAT = "doc,ppt" />

	<cffunction name="createPdf" access="public" returntype="Struct" >
		<cfargument name="source" type="string" required="true" hint="Absolute file path."/>
		<cfargument name="destination" type="string" required="false" default="" hint="Absolute file path." />
		<cfset var result = structNew() />
		<cfset var fileName = "" />
		
		<cfset arguments.source = getAbsolutePath( arguments.source ) />
		
		<cftry>
		
			<cfset validatePermission( "createPdf" ) />
		
			<cfif not listFindNoCase( variables.STATIC.FILE_FORMAT, listLast( arguments.source, '.' ) )>
				<cfthrow message="File type not supported." detail="CreatePdf plug-in accept only DOC files." />
			</cfif>
			
			<cfif not len( arguments.destination )>
				<cfset arguments.destination = reReplaceNoCase( arguments.source, '.doc|.ppt', '.pdf', 'all' ) />
			<cfelse>
				<cfset arguments.destination = getAbsolutePath( reReplaceNoCase( arguments.destination, '.doc|.ppt', '.pdf', 'all' ) ) />
			</cfif>
			
			<cflock name="#arguments.source#" type="readonly" timeout="30" throwontimeout="true" >
			
				<cfdocument format="PDF" srcfile="#arguments.source#" 
										 filename="#arguments.destination#" overwrite="true" />
			
			</cflock>
			
			<cfset result['success'] = true />
									 
			<cfcatch type="any">
				
				<cfset result['success'] = false />
                <cfset result['type'] = "it.millemultimedia.fileManager.plugins.PdfCreator" />
                <cfset result['detail'] = cfcatch.detail />
                <cfset result['message'] = cfcatch.message />
            	
				<cfset variables.eventManager.notifyOnError( error = result ) />
				
			</cfcatch>
		
		</cftry>
		
		<cfreturn result/>
	</cffunction>

</cfcomponent>